<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<!-- <sc-export-button :auth="'contract.detailsExport'" :apiObj="shopObj" :search="search"></sc-export-button> -->
                <el-button type="warning" :auth="'contract.detailsExport'"  @click="batch_export">导出</el-button>
				<!-- 打印-->
				<sc-print-button :auth="'contract.print'" :selection="selection"></sc-print-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-date-picker v-model="search.open_single" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"/>
					<el-input v-model="search.contract_code" placeholder="请输入采购单号" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.customer_name" placeholder="请输入供应商名称" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.goods_name" placeholder="请输入商品名称/商品规格" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.remark" placeholder="备注" clearable @keyup.enter="upsearch"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" row-key="id" :apiObj="shopObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border tableName="produceShopTable" :column="defaultColumn" @row-dblclick="table_edit" :params="query">
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                <template #apply_name="scope">
                    {{scope.row.apply && scope.row.apply.apply_name}}
				</template>
				<template #is_deliver="scope">
					<el-tag type="danger" v-if="scope.row.send_status == 0"> 未发货</el-tag>
					<el-tag type="success" v-if="scope.row.send_status == 1">发货</el-tag>
				</template>
				<template #is_ticket="scope">
					<el-tag :type="scope.row.is_ticket == 2 ? 'primary' : 'danger'">
						{{ scope.row.is_ticket_string }}
					</el-tag>
				</template>
				<template #status="scope">
					<el-tag type="warning" v-if="scope.row.status == 0">待审核</el-tag>
					<el-tag type="success" v-if="scope.row.status == 1">通过</el-tag>
					<el-tag type="danger" v-if="scope.row.status == 2">驳回</el-tag>
				</template>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
	<save-production v-if="dialog.production" ref="productionDialog" @success="handleSuccess" @closed="dialog.production = false"></save-production>
	<plan-save v-if="dialog.plan" ref="planDialog" @success="handleSuccess" @closed="dialog.plan = false"></plan-save>
</template>

<script>
import SaveProduction from '@/views/production/plan/save.vue'
import saveDialog from './save'
import planSave from '@/views/procure/plan/save.vue'
import config from "@/config"
export default {
	name: 'BOM列表',
	components: {
		saveDialog, SaveProduction, planSave
	},
	data() {
		return {
			dialog: {
				save: false,
				production: false,
				plan: false,
			},
			shopObj: this.$API.procure.order.goodsLists,
			selection: [],
			search: {
				open_single: null,
				contract_code: null,
				customer_name: null,
				goods_name: null,
				status: null,
				remark:null
			},
			config:config,
			defaultColumn: [
				{label: "采购单号",prop: "contract_code",width: "140"},
				{label: "开单日期",prop: "open_single",width: "100"},
				{label: "供应商",prop: "apply_name",width: "200"},
				{label: "商品名称",prop: "goods_name",width: "170"},
				{label: "规格",prop: "goods_spec",width: "170"},
				{label: "数量",prop: "sale_num",width: "70"},
				{label: "价格",prop: "price",width: "96"},
				{label: "金额",prop: "total_price",width: "80"},
				{label: "备注",prop: "remark",width: "125"},
				{label: "订单号码",prop: "order_number",width: "122"},
				{label: "修改日期",prop: "update_time",width: "150"},
				{label: "是否送货",prop: "is_deliver",width: "100"},
				{label: "是否已开票",prop: "is_ticket",width: "100"},
				{label: "审核状态",prop: "status",width: "100"},
				{label: "备注2",prop: "remark2",width: "125"},
				// {label: "备注2",prop: "",width: "100"},
			],
            edit:this.$TOOL.data.get("PERMISSIONS").includes('contract.edit'),
            query: {
                type: 2
            },
		}
	},
	mounted() {

	},
	methods: {
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
		async table_copy(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('copy').setData(row, 1)
			})
		},
        table_edit(row) {
			if(this.edit && row.status != 1){
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('edit').setData(row)
                })
            }else{
                this.$alert("没有编辑权限", "提示", {type: 'error'})
            }
		},
        async batch_export() {
            var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
			var reqData = {ids: ids,export:2,}
			var res = await this.$API.procure.order.goodsLists.get(reqData);
			if (res.code == 2) {
				this.$message.success("导出成功");
				window.location.href = res.data.url
			} else {
				this.$alert(res.message, "提示", {type: "error"});
			}
		},
	}
}
</script>

<style></style>
